package com.asion.vmovie.adapters;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.asion.vmovie.R;
import com.asion.vmovie.beans.DetialsBean;
import com.bumptech.glide.Glide;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by asion on 16-10-18.
 */

public class NewsViewAdapter extends RecyclerView.Adapter<NewsViewAdapter.MyHolder> {

    public static final int HEADER=0x10;
    public static final int NORMAL=0x20;

    public static View mHeaderView;

    public View getmHeaderView() {
        return mHeaderView;
    }

    public void setmHeaderView(View mHeaderView) {
        this.mHeaderView = mHeaderView;
        notifyItemInserted(0);
    }

    private Context context;

    private List<DetialsBean.DataBean> list;
    private OnItemClickListener onItemClickListener;

    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
    }

    public NewsViewAdapter(Context context) {
        this.context = context;
        list = new ArrayList<>();
    }

    @Override
    public MyHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        if(mHeaderView != null && viewType == HEADER) return new MyHolder(mHeaderView);

        View view = LayoutInflater.from(context).inflate(R.layout.news_item, parent, false);
        return new MyHolder(view);
    }

    @Override
    public int getItemViewType(int position) {
        if(mHeaderView == null) return NORMAL;
        if(position == 0) return HEADER;
        return NORMAL;
    }

    @Override
    public void onBindViewHolder(MyHolder holder, int position) {
        if(getItemViewType(position) == HEADER) return;

        if(holder != null){
            DetialsBean.DataBean dataBean = list.get(getRealPosition(holder));
            Glide.with(context).load(dataBean.getImage()).asBitmap()
                    .centerCrop()
                    .animate(R.anim.zoom_in).error(R.mipmap.default_error)
                    .into(holder.newsImage);
            holder.newsTitle.setText(dataBean.getTitle());
            holder.newsSubtitle.setText(dataBean.getCates().get(0).getCatename() +"  / "
                    +Integer.parseInt(dataBean.getDuration())/60+"'"+Integer.parseInt(dataBean.getDuration())%60+"\"");
            holder.itemView.setOnClickListener(v->onItemClickListener.onItemClick(dataBean));
        }
    }

    public int getRealPosition(RecyclerView.ViewHolder holder){
        int position = holder.getLayoutPosition();
        return mHeaderView == null ? position : position - 1;
    }

    @Override
    public int getItemCount() {
        return mHeaderView==null?list.size():list.size()+1;
    }

    public void addAll(Collection collection) {
        list.addAll(collection);
        notifyDataSetChanged();
    }

    public void removeAll() {
        list.clear();
        notifyDataSetChanged();
    }

    public static class MyHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.news_image)
        ImageView newsImage;
        @BindView(R.id.news_title)
        TextView newsTitle;
        @BindView(R.id.news_subtitle)
        TextView newsSubtitle;
        public MyHolder(View itemView) {
            super(itemView);

            if(itemView == mHeaderView) return;

            ButterKnife.bind(this,itemView);
        }
    }

    public interface OnItemClickListener {
        void onItemClick(DetialsBean.DataBean dataBean);
    }
}
